from cold_start_analyzer.base_analyzer import BaseAnalyzer
from cold_start_analyzer.consts import NOT_AVAILABLE
from cold_start_analyzer import db_helper
from cold_start_analyzer.log_helper import dlog


class Phase5All(BaseAnalyzer):
    description = "网络请求: H:doRequest从开始到结束"
    """
    起点： app_thread
    H:OnVsyncCallback 结束
    终点： app_thread
    H:doRequest 结束
    """
    def get_result(self):
        if 'Phase4All' not in self.context:
            dlog.error(f"Phase4All not in context: {self.context}")
            return NOT_AVAILABLE
        res = db_helper.get_function_cost(self.cursor, 'H:doRequest', self.context['Phase4All']['start_ts'], None, thread_name=self.thread_name)
        if not res:
            dlog.warning(f"doRequest marker not found for thread {self.thread_name}")
            return NOT_AVAILABLE
        self.context['Phase5All'] = {
            'start_ts': res[0][0],
            'end_ts': res[0][1],
        }
        return res[0][1] - res[0][0]
